package com.tanhua.server.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TodayBestService {
    @Autowired
    private UserService userService;
    @Autowired
    private RecommendUserService recommendUserService;
    @Autowired
    private UserInfoService userInfoService;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    public TodayBest queryTodayBest() {
        // 检验tocken是否有效
        User user = UserThreadLocal.get();
        // 查询今日佳人
        TodayBest todayBest = recommendUserService.queryTodayBest(user.getId());
        if (null == todayBest){
            // 给出默认的用户
            todayBest = new TodayBest();
            todayBest.setId(defaultUser);
            todayBest.setFateValue(80L);
        }
        // 补全推荐佳人的个人信息
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(todayBest.getId());
        if (null == userInfo){
            return null;
        }
        todayBest.setAvatar(userInfo.getLogo());// 头像
        todayBest.setNickname(userInfo.getNickName());// 昵称
        todayBest.setTags(StringUtils.split(userInfo.getTags(),","));// 标签
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man":"woman");// 性别
        todayBest.setAge(userInfo.getAge());// 年龄

        return todayBest;
        }

        public PageResult queryRecommendation(RecommendUserQueryParam queryParam) {
            // 检验token是否有效
            User user = UserThreadLocal.get();

            PageResult pageResult = new PageResult();
            pageResult.setPage(queryParam.getPage());
            pageResult.setPagesize(queryParam.getPagesize());

            PageInfo<RecommendUser> pageInfo = recommendUserService.queryRecommendUserList(user.getId(), queryParam.getPage(), queryParam.getPagesize());
            List<RecommendUser> records = pageInfo.getRecords();
            if (CollectionUtils.isEmpty(records)){
                // 没有查询到推荐用户的数据列表
                return pageResult;
            }
            // 补全推荐用户的个人信息
            // 收集用户的id
            HashSet<Long> userId = new HashSet<>();
            HashMap<String,Long> map = new HashMap();
            for (RecommendUser record : records) {
                userId.add(record.getUserId());
                double score = Math.floor(record.getScore());
                map.put("score", (long) score);
            }

            // 构造条件查询
            QueryWrapper<UserInfo> queryWapper = new QueryWrapper<>();
            queryWapper.in("user_id",userId);
            if (StringUtils.isNoneEmpty(queryParam.getCity())){
                //queryWapper.like("city",queryParam.getCity());
            }
            if (StringUtils.isNoneEmpty(queryParam.getGender())){
                //queryWapper.eq("sex",StringUtils.equals(queryParam.getGender(),"man") ? 1 : 2);
            }
            if (queryParam.getAge() !=null){
                //queryWapper.le("age",queryParam.getAge());
            }
            List<UserInfo> userInfoList = userInfoService.queryUserInfoList(queryWapper);

            if (CollectionUtils.isEmpty(userInfoList)){
                // 没有查询到推荐用户
                return pageResult;
            }

            ArrayList<TodayBest> todayBests = new ArrayList<>();
            TodayBest todayBest = new TodayBest();
            for (UserInfo userInfo : userInfoList) {
                todayBest.setId(userInfo.getId());//id
                todayBest.setAvatar(userInfo.getLogo());//头像
                todayBest.setNickname(userInfo.getNickName());//昵称
                todayBest.setTags(StringUtils.split(userInfo.getTags(),","));//标签
                todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");//性别
                todayBest.setAge(userInfo.getAge());//年龄
                todayBest.setFateValue(map.get("score"));

                todayBests.add(todayBest);
            }
            //按照缘分值进行倒序排序
            Collections.sort(todayBests, (o1, o2) -> new Long(o2.getFateValue() - o1.getFateValue()).intValue());

            pageResult.setItems(todayBests);
            return pageResult;
        }
}
